<?php
/**
 * Created by PhpStorm.
 * User: lihao
 * Date: 2017/7/20
 * Time: 下午4:03
 */

class Api extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('Photo_model');
        $this->load->model('User_model');
    }

    public function createdir() {
        //处理跨域
        $dir = $_POST['dir'];
        $path = 'photos/'.$dir;
        $msg = "创建成功";
        if (is_dir($path)) {
            $msg = "文件夹已存在";
        } else {
            $res = mkdir($path, 0777, true);
            if ($res) {
                $result = $this->Photo_model->createDir($dir);
                if ($result) {
                    $msg = "创建成功";
                } else {
                    $msg = "插入数据库失败";
                }
            } else {
                $msg = "创建失败";
            }
        }
        $this->echoJson('', $msg, 1);
    }

//    获取所有图片文件夹

    public function getDirs($dir, $typeid) {
        $result = $this->Photo_model->getDir();
        $this->echoJson($result, '获取成功', 1);
    }

    public function upload() {
        $dir = $_POST['dir'];
        $config['upload_path']      = urldecode('./'.$dir);
        $config['allowed_types']    = 'gif|jpg|png';
        $config['max_size']     = 1024;
        $config['max_width']        = 1024;
        $config['max_height']       = 768;

        $this->load->library('upload', $config);

        if ( ! $this->upload->do_upload($_POST['data']))
        {
            $error = array('error' => $this->upload->display_errors());
            echo json_encode(array(
                'msg'=>$error
            ));
        }
        else
        {
            $data = array('upload_data' => $this->upload->data());

            if ($data) {
                $fileName = $data['upload_data']['file_name'];
                echo json_encode(array(
                    'msg'=>"成功"
                ));
//                $re = $this->Photo_model->saveImg($fileName, 1, $typeid);
//                if ($re) {
//                    echo json_encode(array(
//                        'msg'=>"成功"
//                    ));
//                } else {
//                    echo json_encode(array(
//                        'msg'=>"失败"
//                    ));
//                }
            }
        }
    }


    public function do_upload()
    {
        //上传配置
        $config['upload_path']      = './images/';
        $config['allowed_types']    = 'gif|jpg|png';
        $config['max_size']     = 2048;

        //加载上传类
        $this->load->library('upload', $config);

        //执行上传任务
        if($this->upload->do_upload('userfile')){
            echo '上传成功';  //如果上传成功返回成功信息
        }
        else{
            echo '上传失败，请重试'; //如果上传失败返回错误信息
        }
    }

    /*
     *
     * 注册相关
     *
     * */

    public function register() {
        $user = $_POST['user'];
        $pw = $_POST['pw'];
        $re = $this->User_model->search($user);
        $msg = "";
        $succ = 0;
        if (!$re) {
            $data = array(
                'user'=>$user,
                'password'=>$pw
            );
            // true false
            $re = $this->User_model->create($data);
            if ($re) {
                $msg = "创建用户成功";
                $succ = 1;
            } else {
                $msg = "创建用户失败";
                $succ = 0;
            }
        } else {
            $msg = "该账号已存在";
            $succ = 0;
        }
        $this->echoJson("", $msg, $succ);
    }

    public function login() {
        $user = $_POST['user'];
        $pw = $_POST['pw'];

        $result = $this->User_model->search($user, null);
        $msg = "";
        $succ = 0;
        if ($result) {
            $password = $result[0]['password'];
            if ($pw == $password) {
                $msg = "";
                $succ = 1;
                $uid = $result[0]['uid'];
                $name = $result[0]['name'];
                $sex = $result[0]['sex'];
                $logo = $result[0]['logo'];

                set_cookie('name', $name, 60 * 60 * 24);
                set_cookie('username', $user, 60 * 60 * 24);
                set_cookie('password', $pw, 60 * 60 * 24);
                set_cookie('uid', $uid, 60 * 60 * 24);
                set_cookie('sex', $sex, 60 * 60 * 24);
                set_cookie('logo', $logo, 60 * 60 * 24);
            } else {
                //
                $msg = "密码错误";
                $succ = 0;
            }
        } else {
            //
            $msg = "该账号不存在";
            $succ = 0;
        }
        $this->echoJson("", $msg, $succ);
    }

    public function logout() {
        delete_cookie('name');
        delete_cookie('username');
        delete_cookie('password');
        delete_cookie('uid');
        delete_cookie('sex');
        $this->echoJson("", '', 1);
    }
}